import { Directive, DirectiveBinding } from 'vue';
// import { useStore } from "vuex"
import useStore from '@/store'

// 使用了类型进行注册
const permission: Directive = {
  // mounted :Directive挂载生命周期
  mounted: (el: HTMLElement, bind: DirectiveBinding) => {
    // const store = useStore();
    // roleData需要自己去引入
    let {user} = useStore()
    const roleData =  user.getPerm();
    const { value } = bind;
    // 判断是否为超级管理员
    // const isAdmin = store.isAdmin??false;
    // if (isAdmin) return;
    //判空以及判断类型 
    if (value) {
      if (value.length > 0) {
        let hasPermission = false;
        const permissionRoles = value;
        // 判断是否存在权限
        
            hasPermission = roleData.some((role) => {
                return permissionRoles.includes(role);
              });
        
        
        // 返回值为false 删除节点
        if (!hasPermission){
            return el.parentNode && el.parentNode.removeChild(el);
        }
      }
    } else {
    //   throw new Error('权限值为空或权限值类型错误');
    }
  }
};

export default permission;